package dimension.starry.item.tool;

import dimension.starry.constants.PlayerDataType;
import dimension.starry.dataManager.PlayerDataManager;
import dimension.starry.effect.Effects;
import dimension.starry.item.typeInterface.ToolType;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class LeatherKettle extends ToolType {
    private static final String WATER_TYPE = "watertype";
    private static final int WATERTYPE_NONE = 0;
    private static final int WATERTYPE_FRESHWATER = 1;
    private static final int WATERTYPE_SEAWATER = 2;
    private static final int DRINK_COUNT = 3;
    private static final int DRINK_ADD = 3000;

    public LeatherKettle() {
        super(new FabricItemSettings().maxDamage((DRINK_COUNT))
          .maxDamageIfAbsent(DRINK_COUNT)
          .food(new FoodComponent.Builder().hunger(0).saturationModifier(0).alwaysEdible().build()));
    }

    @Override
    public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity miner) {
        return false;
    }

    @Override
    public boolean isEnchantable(ItemStack stack) {
        return false;
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack itemstack = playerEntity.getStackInHand(hand);
        BlockHitResult blockHitResult = raycast(world, playerEntity, RaycastContext.FluidHandling.SOURCE_ONLY);
        if (blockHitResult.getType() == HitResult.Type.BLOCK) {
            BlockPos blockpos = blockHitResult.getBlockPos();
            if (world.canPlayerModifyAt(playerEntity, blockpos)) {
                if (world.getBlockState(blockpos).getBlock().getTranslationKey().contains("water")) {
                    NbtCompound nbtCompound = itemstack.getOrCreateNbt();
                    if (world.getBiome(blockpos)
                        .matches(biomeRegistryKey -> biomeRegistryKey.getRegistry().getPath().contains("ocean"))) {
                        nbtCompound.putInt(WATER_TYPE, WATERTYPE_SEAWATER);
                    } else {
                        nbtCompound.putInt(WATER_TYPE, WATERTYPE_FRESHWATER);
                    }
                    itemstack.setNbt(nbtCompound);
                    itemstack.setDamage(0);
                    return TypedActionResult.pass(itemstack);
                }
            }
        }
        if (itemstack.getDamage() >= DRINK_COUNT) {
            NbtCompound nbtCompound = itemstack.getOrCreateNbt();
            nbtCompound.putInt(WATER_TYPE, WATERTYPE_NONE);
            itemstack.setNbt(nbtCompound);
            return TypedActionResult.fail(itemstack);
        }
        playerEntity.setCurrentHand(hand);
        return TypedActionResult.consume(itemstack);
    }

    @Override
    public ItemStack finishUsing(ItemStack itemStack, World world, LivingEntity livingEntity) {
        if (itemStack.getDamage() < DRINK_COUNT && livingEntity instanceof ServerPlayerEntity player) {
            NbtCompound nbtCompound = itemStack.getOrCreateNbt();
            int i = nbtCompound.getInt(WATER_TYPE);
            if (i == WATERTYPE_FRESHWATER && world instanceof ServerWorld) {
                PlayerDataManager.setServerPlayerData(
                    player, PlayerDataType.WATER, PlayerDataManager.getInt(player, PlayerDataType.WATER) + DRINK_ADD);
            } else if (i == WATERTYPE_SEAWATER && world instanceof ServerWorld) {
                livingEntity.addStatusEffect(new StatusEffectInstance(Effects.THIRSTY.getStatusEffect(), 5000, 0));
                PlayerDataManager.setServerPlayerData(
                    player, PlayerDataType.WATER, PlayerDataManager.getInt(player, PlayerDataType.WATER) + DRINK_ADD);
            }
            itemStack.setNbt(nbtCompound);
            itemStack.setDamage(itemStack.getDamage() + 1);
        }
        return itemStack;
    }

    @Override
    public UseAction getUseAction(ItemStack itemStack) {
        return UseAction.DRINK;
    }

    @Override
    public boolean isFood() {
        return true;
    }

    @Override
    public int getMaxUseTime(ItemStack itemStack) {
        return 32;
    }

}
